<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑业务' : '新增业务'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item label="业务编码">
          <a-input
            v-decorator="[
              'code',
              {
                initialValue: model?.code ,
                rules: [{ required: true, message: '请输入业务编码' }],
              },
            ]"
            placeholder="请输入业务编码"
          />
        </a-form-item>
        <a-form-item label="业务名称">
          <a-input
            v-decorator="[
              'name',
              {
                initialValue: model?.name ,
                rules: [{ required: true, message: '请输入业务名称' }],
              },
            ]"
            placeholder="请输入业务名称"
          />
        </a-form-item>
        <a-form-item label="业务分类">
          <a-select
            v-decorator="[
              'clsId',
              {
                initialValue: model?.clsId ,
                rules: [{ required: true, message: '请选择' }],
              },
            ]"
            placeholder="请选择"
          >
            <a-select-option :value="i.id" v-for="i in option" :key="i.id">{{ i.name }}/{{ i.operatorName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="酬金类型">
          <a-select
            v-decorator="[
              'remType',
              {
                initialValue: model?.remType ,
                rules: [{ required: true, message: '请选择' }],
              },
            ]"
            v-model="remType"
            placeholder="请选择"
          >
            <a-select-option value="0">固定值</a-select-option>
            <a-select-option value="1">自填</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="酬金" v-show="remType === '1'">
          <a-input v-decorator="['remValue', { initialValue: model?.remValue }]" placeholder="请输入酬金" />
        </a-form-item>
        <a-form-item label="积分">
          <a-input v-decorator="['integral', { initialValue: model?.integral }]" placeholder="请输入积分" />
        </a-form-item>
        <a-form-item label="扣款方式">
          <a-select
            v-decorator="[
              'dedType',
              {
                initialValue: model?.dedType,
                rules: [{ required: true, message: '请选择' }],
              },
            ]"
            v-model="dedType"
            placeholder="请选择"
          >
            <a-select-option value="0">不扣款</a-select-option>
            <a-select-option value="1">固定值</a-select-option>
            <a-select-option value="2">按比例</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="扣款值" v-show="dedType === '2' || dedType === '3'">
          <a-input
            v-decorator="[
              'dedValue',
            ]"
            placeholder="请输入酬金"
          />
        </a-form-item>
        <a-form-item label="提成方式">
          <a-select v-decorator="['propType', { initialValue: model?.propType }]" placeholder="请选择">
            <a-select-option value="0">固定值</a-select-option>
            <a-select-option value="1">按酬金比例</a-select-option>
            <a-select-option value="2">按毛利比例</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="提成值">
          <a-input v-decorator="['propValue', { initialValue: model?.propValue }]" placeholder="请输入酬金" />
        </a-form-item>
        <a-form-item label="会员积分">
          <a-input v-decorator="['memIntegral', { initialValue: model?.memIntegral }]" placeholder="请输入酬金" />
        </a-form-item>
        <a-form-item label="重复办理">
          <a-select v-decorator="['isRepeat', { initialValue: model?.isRepeat }]" placeholder="请选择">
            <a-select-option value="0">当月不可重复</a-select-option>
            <a-select-option value="1">当月可重复</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea rows="2" v-decorator="['memo', { initialValue: model?.memo }]" placeholder="请输入备注" />
        </a-form-item>
        <a-form-item label="状态" v-show="model && model?.id > 0">
          <a-select v-decorator="['busiStatus', { initialValue: model?.busiStatus }]" placeholder="状态">
            <a-select-option :value="1">使用中</a-select-option>
            <a-select-option :value="0">已停用</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getOpBusinessClassListUsingPOST } from '@/api/api/yunyingshangguanli.ts'
import pick from 'lodash.pick'
// 表单字段
const fields = ['code','name','clsId','remType','remValue','integral','dedValue','memIntegral','propValue','isRepeat','memo','', 'busiStatus', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
      option: [],
      remType: '1',
      dedType: '1',
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
        classType: 1,
      },
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      console.log(this.model)
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted() {
    this.getOption()
  },
  methods: {
    getOption() {
      getOpBusinessClassListUsingPOST(this.optionParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
